import java.io.IOException;
import java.net.Socket;

public class Client {
    public  Closer closer;
    private Socket socket;

    public Client(String host, int port) throws IOException {
        this.socket = new Socket(host, port);
        closer = new Closer(socket);

    }

    public void start() {
        new ClientSendThread(socket, new Message("start")).start(); // 给服务器发一个开始运行的消息
        ClientReadThread readThread = new ClientReadThread(socket);
        readThread.setDaemon(true);
        readThread.start();
        ClientExamSystem ces = new ClientExamSystem(socket);
        boolean launch;
        launch = ces.launch(readThread);

        if (launch) {
            System.out.println("正在断开连接...");
            while (!socket.isClosed()) {
                try {
                    Thread.sleep(1);
                    closer.close();
                    Thread.sleep(1);
                    System.exit(0);
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

